package com.ll.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ll.blog.Exception.BlogException;
import com.ll.blog.annotation.CheckWord;
import com.ll.blog.common.ApiResponse;
import com.ll.blog.common.WordFilter;
import com.ll.blog.contant.ExceptionEnum;
import com.ll.blog.entity.SensitiveWord;
import com.ll.blog.service.ISensitiveWordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 敏感词管理接口
 *
 * @author 作者
 * @since 2024-03-08
 */
@RestController
public class SensitiveWordController {

    @Resource
    ISensitiveWordService wordService;
    @Resource
    WordFilter wordFilter;

    /**
     * 获取所有敏感词
     */

    @GetMapping("/word")
    public ApiResponse getAll(String i){
        List<String> all = wordService.getAll();
        return ApiResponse.success(all);
    }

    /**
     * 添加敏感词
     * @param name 敏感词
     * @return
     */

    @PostMapping("/admin/word")
    public ApiResponse add(@NotEmpty(message = "必须传递敏感词") String name){
        SensitiveWord word = new SensitiveWord();
        word.setWord(name);
        boolean saved = wordService.save(word);
        if (!saved){
            return ApiResponse.error(ExceptionEnum.SAVE_ERROR);
        }
        return ApiResponse.success("添加成功");
    }

    /**
     * 删除敏感词
     * @param ids 敏感词id列表
     * @return
     */
    @DeleteMapping("/admin/word")
    @Transactional
    public ApiResponse del(@RequestParam List<Integer> ids){
        boolean b = wordService.removeByIds(ids);
        if (!b){
            throw new BlogException(ExceptionEnum.DELETE_ERROR);
        }
        return ApiResponse.success("删除成功");
    }

    /**
     * 重置敏感词过滤器
     */

    @PutMapping("/admin/reset")
    public ApiResponse Reset(){
        List<String> all = wordService.getAll();
        wordFilter.build(all);
        return ApiResponse.success("更新成功");
    }



}
